﻿// -------------------------------------------------------------------------
//    @FileName         :    NFTransInsertAcc.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-08-17
//    @Email			:    445267987@qq.com
//    @Module           :    NFTransInsertAcc
//    @Desc             :    
//
// -------------------------------------------------------------------------

#include "NFTransInsertAcc.h"

#include "NFOnlineAcc.h"
#include "NFOnlineAccMgr.h"

NFTransInsertAcc::NFTransInsertAcc()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFTransInsertAcc::~NFTransInsertAcc()
{
}

int NFTransInsertAcc::CreateInit()
{
    m_ullUin = 0;
    m_iWorldId = 0;
    m_iWorldType = 0;
    return 0;
}

int NFTransInsertAcc::ResumeInit()
{
    return 0;
}

int NFTransInsertAcc::InsertCacheDataRes(const NFrame::storesvr_insertobj_res* pRes, int32_t errCode)
{
    CHECK_NULL(this->m_ullRoleId, pRes);
    if (errCode != 0)
    {
        LOG_ERR(this->m_ullRoleId, errCode, "Insert Cache Data to Db Failed, errCode:{}", errCode);
        return NFrame::ERR_CODE_SVR_SYSTEM_DATABASE_ERROR;
    }

    // 插入成功后，发起查询验证
    proto_ff::tbRoleList pbCacheData;
    pbCacheData.set_uin(m_ullUin);
    pbCacheData.set_world_id(m_iWorldId);
    pbCacheData.set_world_type(m_iWorldType);
    int iRetCode = FindModule<NFIServerMessageModule>()->SendSelectObjTrans((NF_SERVER_TYPE)this->m_iServerType, this->m_ullTargetId, pbCacheData, this->m_iTableId, this->GetGlobalId());
    CHECK_ERR(this->m_ullRoleId, iRetCode, "SendSelectObjTrans Failed, ullId:{}", this->m_ullTargetId);
    return 0;
}

int NFTransInsertAcc::InsertCacheDataReq(uint64_t ullId, int iReason, const google::protobuf::Message& stPbClass)
{
    auto pRoleList = dynamic_cast<const proto_ff::tbRoleList*>(&stPbClass);
    CHECK_NULL(ullId, pRoleList);

    m_ullTargetId = ullId;
    m_iReason = iReason;
    m_ullUin = pRoleList->uin();
    m_iWorldId = pRoleList->world_id();
    m_iWorldType = pRoleList->world_type();
    int iRetCode = FindModule<NFIServerMessageModule>()->SendInsertObjTrans((NF_SERVER_TYPE)m_iServerType, ullId, stPbClass, this->m_iTableId, this->GetGlobalId());
    CHECK_ERR(m_ullRoleId, iRetCode, "SendInsertObjTrans Failed, roleId:{}", m_ullRoleId);
    return 0;
}
